package com.leigq.www.mybatisplusstudy.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.leigq.www.mybatisplusstudy.domain.entity.Department;
import com.leigq.www.mybatisplusstudy.domain.entity.Staff;
import com.leigq.www.mybatisplusstudy.domain.mapper.StaffMapper;
import com.leigq.www.mybatisplusstudy.domain.service.IStaffService;
import com.leigq.www.mybatisplusstudy.domain.service.impl.base.MyServiceImpl;
import com.leigq.www.mybatisplusstudy.enums.GenderEnum;
import com.leigq.www.mybatisplusstudy.param.AddStaffsParam;
import com.leigq.www.mybatisplusstudy.vo.ListStaffsCustomVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 员工服务实现类
 * </p>
 *
 * @author leigq
 * @since 2019-06-14
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(rollbackFor = Exception.class)
public class StaffServiceImpl extends MyServiceImpl<StaffMapper, Staff> implements IStaffService {

    /**
     * 添加员工
     *
     * @return the boolean
     */
    @Override
    public Boolean saveStaffs(AddStaffsParam addStaffsParam) {
        Staff s;
        final List<Staff> staffs = Lists.newArrayList();
        for (int i = 0; i < 2; i++) {
            s = Staff.builder().build();
            BeanUtil.copyProperties(addStaffsParam, s);
            // 填充好其它字段，如果个别字段在 entity 里为 null 但是数据库中有配置默认值, insert 后数据库字段是为 null 而不是默认值
            staffs.add(s);
        }
        return super.saveBatchSomeColumn(staffs);
    }


    /**
     * 修改员工信息
     *
     * @param id the id
     * @return the boolean
     */
    @Override
    public Boolean updateStaffs(Long id) {
        // 先查询，才能使用乐观锁
        final Staff staff = super.getById(id);
        if (Objects.isNull(staff)) {
            throw new RuntimeException("员工不存在");
        }
        Staff s = Staff.builder().id(id).age(20).version(staff.getVersion()).build();
        return super.updateById(s);
    }


    /**
     * 删除员工，逻辑删除
     *
     * @param id the id
     * @return the boolean
     */
    @Override
    public Boolean deleteStaffs(Long id) {
        return super.removeById(id);
    }


    /**
     * 查询员工列表
     *
     * @return the boolean
     */
    @Override
    public List<Staff> listStaffs() {
        return super.list();
    }

    /**
     * 查询员工列表，根据性别
     *
     * @return the list
     */
    @Override
    public List<Staff> listStaffsByGender(GenderEnum gender) {
        return super.lambdaQuery().eq(Staff::getGender, gender).list();
    }

    /**
     * 查询员工列表，使用 Wrapper 自定义SQL
     *
     * @return the list
     */
    @Override
    public IPage<ListStaffsCustomVO> listStaffsCustom() {
        // 不支持 Wrapper 内的entity生成where语句, select查询条件也不生效，只是where条件生效，逻辑删除也需要自己加上
        return baseMapper.listStaffsCustom(
                new Page<>(1, 10),
                Wrappers.query()
                        .eq(Department.TableColumns.NAME, "技术部")
                        .gt(Staff.TableColumns.AGE, 10)
                        .eq(Staff.TableColumns.DELETED, 0)
        );
    }
}
